package com.ds.smartstudio.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ds.smartstudio.controller.DTO.deviceObj;
import com.ds.smartstudio.entity.Device;
import com.ds.smartstudio.entity.History;
import com.ds.smartstudio.mapper.DeviceMapper;
import com.ds.smartstudio.mapper.HistoryMapper;
import com.ds.smartstudio.service.HistoryService;
//import org.jcp.xml.dsig.internal.SignerOutputStream;
//import netscape.javascript.JSObject;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Array;
import java.sql.SQLOutput;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import com.ds.smartstudio.service.DeviceService;
import com.ds.smartstudio.service.HistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yangsf
 * @since 2022-07-02 10:42:24
 */
@Service
public class HistoryServiceImpl extends ServiceImpl<HistoryMapper, History> implements HistoryService {
    @Autowired
    HistoryMapper historyMapper;

    @Autowired
    DeviceMapper deviceMapper;
    @Override
    public History selectLatestByMac(String mac) {
        return historyMapper.selectLatestByMac(mac);
    }

    @Override
    public List<History> dayOfTemp(String mac) {
        Page<History> page = new Page<>(1, 8);//返回八条历史温度
        QueryWrapper<History> wrapper = new QueryWrapper<>();
        wrapper.eq("ds_device_mac", mac)//根据设备的Mac地址
                .orderByDesc("ds_history_id");//降序排序查询
        this.page(page, wrapper);
        List<History> records = page.getRecords();
        return records;
    }

    @Override
    public List<History> selectLatest() {
        return historyMapper.selectLatest();
    }

    @Override
    public List<History> areaTemp(Integer id) {
        QueryWrapper<History> wrapper = new QueryWrapper<>();
        wrapper.in("ds_device_mac", deviceMapper.tempDevice(id));
        return this.list(wrapper);
    }

    @Override
    public List<History> allOfTemp(String mac) {
        Integer total = historyMapper.selectTotalByMac(mac);
        Page<History> page = new Page<>(1, total);
        QueryWrapper<History> wrapper = new QueryWrapper<>();
        wrapper.eq("ds_device_mac", mac)
                .orderByDesc("ds_history_id");
        this.page(page, wrapper);
        return page.getRecords();
    }
    @Override
    public List historyPage(int page, int pageSize, String mac) {
        Page<History> HistoryPage = new Page<>(page, pageSize);
        QueryWrapper<History> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("ds_history_id")
                .like("ds_device_mac", mac);
        this.page(HistoryPage, wrapper);
        List<History> historys = HistoryPage.getRecords();
        ArrayList<Object> data = new ArrayList<>();
        data.add(historys);
        data.add(historyMapper.selectTotalByMac(mac));
        return data;
    }

    @Override
    public List<String> MacInArea(String area) { return historyMapper.selectMacByArea(area); }


    @Override
    public List<String> MacInType(String Type) {
        Integer typeId = historyMapper.selectTypeIdByTypename(Type);
        List<Device> devices = SerchResultByTypeId(typeId);
        List<String> data=new ArrayList<>();
        for (Device device : devices) {
            data.add(device.getDsDeviceMac());
        }
        return data;
    }

//    通过id查询device表中与之匹配的某条记录
    public List<Device> SerchResultByTypeId(int typeId){
        List<Device> devices = new ArrayList<>();
        List<Device> types = deviceMapper.selectList(null);

        for (Device type : types) {
            List<Integer> dsTypeId = type.getDsTypeId();
//                遍历数组 寻找匹配的typeId
            for (Integer value : dsTypeId) {
                if(typeId==value){
                    devices.add(type);
                }
            }
        }
        return devices;
    }

//    已知集合分页给数据
    public List<History> dataPage(List<History> dataList, int page, int pageSize){
        int pageStart=((page-1)*pageSize)+1;
        int pageTotal=pageSize;
        int flag=1;
        int pageCount=1;
        List<History> dataListPage=new ArrayList<>();
        for (History data : dataList) {
            if(flag>=pageStart&&pageCount<=pageTotal){
                dataListPage.add(data);
                pageCount++;
            }
            if(pageCount>pageTotal){break;}
            flag++;
        }
        return dataListPage;
    }
    @Override
    public List History(String Area,String Mac, String TimeStart, String TimeEnd,int page,int pageSize) {

//        设备名查找设备Id 地区名查找地区Id
        Integer AreaId=null;
        String MAC=null;

        Timestamp timeStart=null;
        Timestamp timeEnd=null;
        if(Area!="") {
            AreaId = historyMapper.selectAreaIdByAreaname(Area);
        }
        if(Mac!=""){
            MAC=Mac;
        }
        if(TimeStart!=""&&TimeEnd!=""){
            timeStart = new Timestamp(System.currentTimeMillis());
            timeStart = Timestamp.valueOf(TimeStart);
            timeEnd = new Timestamp(System.currentTimeMillis());
            timeEnd = Timestamp.valueOf(TimeEnd);
        }
//        前端日期转数据库Timestamp
//        List<History> histories = historyMapper.selectHistory(AreaId, MAC,timeStart,timeEnd);
        List<History> histories = historyMapper.HistoryQuerry(AreaId,MAC,timeStart,timeEnd);
//        根据已有数据集合 分页数据集合
        List<History> historiesPage = dataPage(histories, page, pageSize);
//        全部数据集合长度
        int Selectsize = histories.size();
//        发送整合数据
        List<Object> Querydata=new ArrayList<>();
        Querydata.add(historiesPage);
        Querydata.add(Selectsize);

        return Querydata;
    }
}
